class Solution
{
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals)
    {
        vector<vector<int>> ans;
        sort(intervals.begin(), intervals.end());

        for (auto intr : intervals)
        {
            if (ans.empty() || ans.back()[1] < intr[0])
                ans.push_back(intr);
            else
                ans.back()[1] = max(ans.back()[1], intr[1]);
        }
        return ans;
    }
};